<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/9/01
 * Time: 下午5:31
 */

namespace app\common\model;

use think\Cache;
use think\Db;
use think\exception\DbException;

class Consortium extends Common
{

    /**
     * 联盟商列表
     * @param int $category_id
     * @param string $city
     * @param string $page
     * @param string $page_size
     *
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    static public function consortiumList($category_id=0,$city,$page,$page_size)
    {
        if($category_id>0)
        {
            $list=Db::name('union')
                ->where('category_id',$category_id)
                ->where('in_blacklist',0)
                ->where('is_visible',1)
                ->where('city',$city)
                ->field('id,city,title,thumb,address,tel,longitude,latitude')
                ->page($page,$page_size)
                ->select();
        }else{
            $list=Db::name('union')
                ->where('is_recommend',1)
                ->where('in_blacklist',0)
                ->where('is_visible',1)
                ->where('city',$city)
                ->field('id,city,title,thumb,address,tel,longitude,latitude')
                ->page($page,$page_size)
                ->select();
        }

        return empty($list) ? array() : $list;
    }

    /**
     * 联盟商信息
     * @param int $consortium_id 联盟商id
     *
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    static public function consortiumInfo($consortium_id)
    {
        /*联盟商详情*/
            $info=Db::name('union')
                ->where('id',$consortium_id)
                ->where('in_blacklist',0)
                ->where('is_visible',1)
                ->field('id,uid,city,title,thumb,address,tel,category_id,detail,longitude,latitude,background')
                ->find();
           /*评价情况*/
            $comment=Db('order_comment')
                ->alias('c')
                ->join("__USER__ u","c.uid=u.id")
                ->where('c.union_id',$consortium_id)
                ->field('c.*,u.avatar as avatar,u.mobile as mobile')
                ->order('c.comment_time')
                ->select();
            /*求评价的平均值*/
           $avg=Db('order_comment')
               ->where('union_id',$consortium_id)
               ->avg('star');

            $info['comment']=empty($comment)?array():$comment;
            $info['avg']=$avg;

        return empty($info) ? array() : $info;
    }

    /**
     * 优惠券列表
     * @param string $page
     * @param string $page_size
     *
     * @param string $city
     * @param int $category_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    static public function cardList($page,$page_size,$city,$category_id)
    {
        if($category_id=="-1")
        {
            $list=Db::name('union_discount')
                ->alias('d')
                ->join('__UNION__ u',"d.union_id=u.id","left")
                ->where('u.city',$city)
                ->order('d.display_order')
                ->field('d.id,u.city,u.title,d.thumb as background,u.thumb as thumb,u.address,u.tel,d.discount/10 as discount')
                ->page($page,$page_size)
                ->select();
        }else{
            $list=Db::name('union_discount')
                ->alias('d')
                ->join('__UNION__ u',"d.union_id=u.id","left")
                ->where('d.category_id',$category_id)
                ->where('u.city',$city)
                ->order('d.display_order')
                ->field('d.id,u.city,u.title,d.thumb as background,u.thumb as thumb,u.address,u.tel,d.discount/10 as discount')
                ->page($page,$page_size)
                ->select();
        }



        return empty($list) ? array() : $list;
    }

    /**
     * 联盟商账户状态
     * @param string $uid
     *
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    static public function accountInfo($uid)
    {

        /*联盟商详情*/
        $info=Db::name('union')
            ->where('uid',$uid)
            ->field('id,in_blacklist')
            ->find();

        if(!$info){
            throw new UserException("联盟商不存在！");
        }
        if($info['in_blacklist']==1){
            throw new UserException("联盟商账户已被拉黑，请联系管理员处理！");
        }
        return empty($info) ? array() : $info;
    }

}